﻿using NFine.Data;
using NFine.Domain.Entity.BusinessManage;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.BusinessManage;
using System.Collections.Generic;
using System.Linq;

namespace NFine.Repository.BusinessManage
{
    public class D_GroupMemberRepository : RepositoryBase<D_GroupMemberEntity>, ID_GroupMemberRepository
    {
        //public void DeleteForm(string keyValue)
        //{
        //    using (var db = new RepositoryBase().BeginTrans())
        //    {
        //        db.Delete<UserEntity>(t => t.F_Id == keyValue);
        //        db.Delete<UserLogOnEntity>(t => t.F_UserId == keyValue);
        //        db.Commit();
        //    }
        //}

        public List<Domain.ViewModel.GroupMembersModel> GetGroupMembers(string groupid)
        {
            List<Domain.ViewModel.GroupMembersModel> Members = new List<Domain.ViewModel.GroupMembersModel>();
            using (var db = new RepositoryBase())
            {
                var robjs1 = from gm in db.DbSet<D_GroupMemberEntity>()
                             join u in db.DbSet<UserEntity>() on gm.MemberID equals u.F_Id
                             join u3 in db.DbSet<UserEntity>() on u.ReferenceID equals u3.F_Id into r3
                             from ru in r3.DefaultIfEmpty()
                             join gm2 in db.DbSet<D_GroupMemberEntity>() on gm.ID equals gm2.GroupRef into r
                             from gmr in r.DefaultIfEmpty()
                             join u2 in db.DbSet<UserEntity>() on gmr.MemberID equals u2.F_Id into r2
                             from ur in r2.DefaultIfEmpty()
                             where gm.GroupID == groupid
                             orderby gm.OrderID ascending, gm.PromotionTime, gmr.PromotionTime
                             select new { gm, gmr, u, ur, ru };

                foreach (var item in robjs1)
                {
                    var m = Members.FirstOrDefault(gmm => gmm.GroupMember.ID == item.gm.ID);
                    if (m == null)
                    {
                        Domain.ViewModel.GroupMembersModel model = new Domain.ViewModel.GroupMembersModel();
                        item.gm.User = item.u;
                        item.gm.User.ReferenceName = item.ru?.F_Account;
                        model.GroupMember = item.gm;
                        model.Subordinates = new List<D_GroupMemberEntity>();
                        if (item.gmr != null)
                        {
                            item.gmr.User = item.ur;
                            model.Subordinates.Add(item.gmr);
                        }
                        Members.Add(model);
                    }
                    else
                    {
                        if (item.gmr != null)
                        {
                            item.gmr.User = item.ur;
                            m.Subordinates.Add(item.gmr);
                        }
                    }
                }
            }
            return Members;
        }

    }
}
